package cn.stephen.study.demoproject.controller;

import cn.stephen.study.demoproject.dao.dbtwo.StationEquipmentMapper;
import cn.stephen.study.demoproject.service.StationEquipentService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/stationEquipment")
public class StationEquipmentController {
    @Autowired
    private StationEquipmentMapper stationEquipmentMapper;
    @Autowired
    private StationEquipentService stationEquipentService;

    /**
     * 查询所有站点
     *
     * @return
     */
    @RequestMapping("findAllStations")
    @ResponseBody
    public JSONObject findAllStations() {
        JSONObject jo = new JSONObject();
        try {
//            List<Map<String, String>> allStations = stationEquipmentMapper.findAllStations();
            List<Map<String, String>> list  =new ArrayList<>();
            Map map=new HashMap();
            map.put("STATIONCODE","42098100200104000001");
            map.put("STATIONNAME","云应膏盐Ⅰ号监测网");
            list.add(map);
            Map map1=new HashMap();
            map1.put("STATIONCODE","42098100200604000000");
            map1.put("STATIONNAME","云应膏盐Ⅱ号监测网");
            list.add(map1);
            Map map2=new HashMap();
            map2.put("STATIONCODE","42092310700904000000");
            map2.put("STATIONNAME","云应膏盐Ⅲ号监测网");
            list.add(map2);
            Map map3=new HashMap();
            map3.put("STATIONCODE","42098110800204000000");
            map3.put("STATIONNAME","云应膏盐Ⅴ号监测网");
            list.add(map3);
            Map map4=new HashMap();
            map4.put("STATIONCODE","42098100500104000000");
            map4.put("STATIONNAME","云应膏盐Ⅳ号监测网");
            list.add(map4);
            JSONArray jsonArray = new JSONArray();
            for (Map<String, String> allStation : list) {
                JSONObject jsonObject = new JSONObject(new LinkedHashMap());
                jsonObject.put("stationCode", allStation.get("STATIONCODE"));
                jsonObject.put("stationName", allStation.get("STATIONNAME"));
                jsonArray.add(jsonObject);
            }
            jo.put("data", jsonArray);
            jo.put("total", jsonArray.size());
            jo.put("code", 0);
            jo.put("msg", "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            jo.put("data", null);
            jo.put("total", 10);
            jo.put("code", 0);
            jo.put("msg", "");
        }
        return jo;
    }

    /**
     * 根据条件查询站点设备
     *
     * @return
     */
    @RequestMapping(value = "findByValues")
    @ResponseBody
    public JSONObject findByValues(@RequestParam("stationCode") String stationCode,
                                   @RequestParam("jscj") String jscj,
                                   @RequestParam("equipmentType") Integer equipmentType) {
        JSONObject jo = new JSONObject();
        try {
            List<Map<String, String>> stationEquipments = stationEquipentService.findAll(stationCode, jscj, equipmentType);
            JSONArray jsonArray = new JSONArray();
            for (Map<String, String> map : stationEquipments) {
                JSONObject jsonObject = new JSONObject();
//                jsonObject.put("id", map.getId());
                jsonObject.put("stationCode", map.get("STATIONCODE"));
                jsonObject.put("stationName", map.get("STATIONNAME"));
                jsonObject.put("jscj", map.get("JSCJ204A050"));
                jsonObject.put("equipmentName", map.get("EQUIPMENTNAME"));
                jsonObject.put("equipmentType", map.get("EQUIPMENTTYPE"));
//                jsonObject.put("id", stationEquipment.getId());
//                jsonObject.put("stationCode", stationEquipment.getSTATIONCODE());
//                jsonObject.put("stationName", stationEquipment.getSTATIONNAME());
//                jsonObject.put("jscj", stationEquipment.getJSCJ204A050());
//                jsonObject.put("equipmentName", stationEquipment.getEQUIPMENTNAME());
//                jsonObject.put("equipmentType", stationEquipment.getEQUIPMENTTYPE());
                jsonArray.add(jsonObject);
            }
            jo.put("data", jsonArray);
            jo.put("total", jsonArray.size());
            jo.put("code", 0);
            jo.put("msg", "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            jo.put("data", null);
            jo.put("total", 10);
            jo.put("code", 0);
            jo.put("msg", "");
        }
        return jo;
    }


}
